/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import request from '@/utils/request';
import { isLegalData } from '@/utils/utils';

const { MODE_ENV } = process.env;
const isAutomaticLocal = MODE_ENV === 'AUTOMATIC';

// 分拣线配置，获取分拣线品牌
export async function getSortLineBrands(params) {
  return request('/Api/Automation/SortLine/listBrand', {
    method: 'POST',
    body: params,
  });
}
// 分拣线配置，获取列表
export async function getSortLineList(params) {
  return request('/Api/Automation/SortLine/list', {
    method: 'POST',
    body: params,
  });
}
// 分拣线配置，删除列表
export async function deleteSortLineList(params) {
  return request('/Api/Automation/SortLine/delete', {
    method: 'POST',
    body: params,
  });
}
// 分拣线配置，新增分拣线
export async function addSortLine(params) {
  return request('/Api/Automation/SortLine/save', {
    method: 'POST',
    body: params,
  });
}
// 分拣线配置，授权码列表
export async function getAuthCodeList(params) {
  return request('/Api/Automation/SortLineToken/list', {
    method: 'POST',
    body: params,
  });
}
// 分拣线配置，取消授权码
export async function cancelAuthCode(params) {
  return request('/Api/Automation/SortLineToken/del', {
    method: 'POST',
    body: params,
  });
}
// 分拣线配置，添加授权码
export async function addAuthCode(params) {
  return request('/Api/Automation/SortLineToken/addToken', {
    method: 'POST',
    body: params,
  });
}
// 分拣线配置，校验分拣线编号
export async function checkCode(params) {
  return request('/Api/Automation/SortLine/checkCode', {
    method: 'POST',
    body: params,
  });
}
// 分拣线配置，获取上下线扫描员
export async function getScanner(params) {
  return request('/Api/Automation/SortLine/salesmanList', {
    method: 'POST',
    body: params,
  });
}
// 分拣线配置，设置分拣模式
export async function saveModel(params) {
  return request('/Api/Automation/SortLine/saveModel', {
    method: 'POST',
    body: params,
  });
}

// 格口配置，获取列表
export async function getGridConfList(params) {
  return request('/Api/Automation/GeKou/list', {
    method: 'POST',
    body: params,
  });
}
// 格口配置，导入配置
export async function uploadGridConfig(params) {
  return request('/Api/Automation/GeKou/upload', {
    method: 'POST',
    body: params,
  });
}
// 格口配置，获取分拣线列表
export async function getGridSortLineList(params) {
  const url = isAutomaticLocal
    ? '/admin/SortLine/getLineList'
    : '/Api/Automation/GeKou/getLineList';
  return request(url, {
    method: 'POST',
    body: params,
  });
}
// 格口配置，删除配置
export async function deleteGridConfig(params) {
  return request('/Api/Automation/GeKou/del', {
    method: 'POST',
    body: params,
  });
}
// 格口配置，新增正常格口
export async function addGrid(params) {
  return request('/Api/Automation/GeKou/save', {
    method: 'POST',
    body: params,
  });
}
// 格口配置，新增异常格口
export async function addAbnormalGrid(params) {
  return request('/Api/Automation/GeKou/addType2', {
    method: 'POST',
    body: params,
  });
}
// 格口配置，编辑异常、正常格口
export async function editGrid(params) {
  return request('/Api/YZ/CourierStation/editGrid', {
    method: 'POST',
    body: params,
  });
}
// 格口配置，新增异常、正常格口，网点编号查询
export async function getInnNum(params) {
  return request('/Api/Automation/GeKou/getBranchCode', {
    method: 'POST',
    body: params,
  });
}
// 格口配置，新增正常格口，获取下节点操作对象
export async function getSubOperator(params) {
  return request('/Api/Automation/GeKou/getDownScannerObject', {
    method: 'POST',
    body: params,
  });
}
// 进港分拣，获取列表
export async function getArrivalSortingList(params) {
  const url = isAutomaticLocal
    ? '/admin/SortLine/listArrival'
    : '/Api/Automation/SortLine/listArrival';
  return request(url, {
    method: 'POST',
    body: params,
  });
}
// 格口统计，获取列表
export async function getGridStatisticsList(params) {
  const url = isAutomaticLocal
    ? '/admin/SortLine/codeStatistic'
    : '/Api/Automation/GeKou/codeStatistic';
  return request(url, {
    method: 'POST',
    body: params,
  });
}
// 格口统计，下发配置
export async function dispatchConfig(params) {
  return request('/Api/Automation/GeKou/sysData', {
    method: 'POST',
    body: params,
  });
}
// 地址分拣，地址分拣列表
export async function getAddressSortList(params) {
  return request('/Api/Automation/addrScheme/list', {
    method: 'POST',
    body: params,
  });
}
/** 地址分拣，删除配置 */
export async function deleteAddressSort(params) {
  return request('/Api/Automation/addrScheme/del', {
    method: 'POST',
    body: params,
  });
}
/** 地址分拣，获取格口号 */
export async function getGridCode(params) {
  return new Promise(resolve => {
    request('/Api/Automation/GeKou/list', {
      method: 'POST',
      body: params,
    })
      .then(res => {
        const { code, data } = res;
        const { list } = data || {};
        if (code == 0) {
          resolve(isLegalData(list));
        } else {
          resolve([]);
        }
      })
      .catch(() => {
        resolve([]);
      });
  });
}

/** 地址分拣，新增、修改配置 */
export async function addNewConfig(params) {
  return request('/Api/Automation/addrScheme/save', {
    method: 'POST',
    body: params,
  });
}
/** 地址分拣，获取已使用的优先级 */
export async function getUsedSortList(params) {
  return request('/Api/Automation/addrScheme/sortList', {
    method: 'POST',
    body: params,
  }).then(({ data } = {}) => isLegalData(data));
}
/** 判断是否是老用户 */
export async function isOldUser(params) {
  return request('/Api/Automation/SortLine/config', {
    method: 'POST',
    body: params,
  }).then(({ data } = {}) => data.isOldUser == 1);
}
